﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Cngot.Extensions.EventBus.RabbitMQ;
using Cngot.Extensions.EventBus.RabbitMQ.Integration;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace WebApi.Producer.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class HomeController : ControllerBase
    {
        IRabbitProducerFactory rabbitProducerFactory;
        ILogger<HomeController> logger;

        public HomeController(IRabbitProducerFactory rabbitProducerFactory, ILogger<HomeController> logger)
        {
            this.logger = logger;
            this.rabbitProducerFactory = rabbitProducerFactory;
        }

        /// <summary>
        /// 日志
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        [HttpGet]
        public string Get(string message)
        {
            logger.LogTrace($"Trace:{message}");
            logger.LogDebug($"Debug:{message}");
            logger.LogInformation($"Information:{message}");
            logger.LogWarning($"Warning:{message}");
            logger.LogError($"Error:{message}");
            logger.LogCritical($"Critical:{message}");

            return "success";
        }

        /// <summary>
        /// Direct
        /// </summary>
        /// <param name="route">路由</param>
        /// <param name="message">消息</param>
        /// <param name="persistent">消息是否持久化</param>
        /// <returns></returns>
        [HttpGet("Direct")]
        public async Task<string> Direct(string route = "direct1", string message = "Direct", bool persistent = true)
        {
            var producer = rabbitProducerFactory.Create("rabbit-direct");
            await producer.PublishAsync(route, message, new MessageOptions() { Persistent = persistent });

            return "success";
        }


        [HttpGet("DirectDelay")]
        public async Task<string> DirectDelay(string route = "direct-delay", string message = "DirectDelay", bool persistent = true)
        {
            var producer = rabbitProducerFactory.Create("rabbit-delay");
            await producer.PublishDelayAsync(TimeSpan.FromSeconds(5), route, message + ">>" + DateTime.Now, new MessageOptions() { Persistent = persistent });

            return "success";
        }
    }
}
